#!/usr/bin/env perl

# vim: ts=4:sw=4:et:syntax=perl
BEGIN {
    push @INC,"../libs";
}

# This is a test client - it will load in the following details:
# Private key - from client/privkeys/client.private

use gri2_rsatcp;
use Data::Dumper;
use Socket;
use Carp;

$::private=undef;
{
    my ($a,$b)=gri2_rsatcp::load_private_key({file=>"client/privkeys/client.private"});
    if(!$a) {
        print STDERR "$_\n" foreach(@$b);
        exit(1);
    }
    $::private=$b;
}

# Load all public keys from expected directory...

# $::publics=undef;
# {
    # my ($a,$b,$c)=gri2_rsatcp::load_all_public_keys({dir=>"client/pubkeys"});
    # if(!$a) {
        # print STDERR "$_\n" foreach(@$b);
        # exit(1);
    # }
    # $::publics=$b;
# }

# Find the public we need to send requests to our server...

# $::my_server_pub_key=undef;
# {
    # my ($a,$b)=gri2_rsatcp::find_pub_key({
        # keys => $::publics,
        # "username/host" => 'test1/host1',
    # });
    # if(!$a) {
        # print STDERR "$_\n" foreach(@$b);
        # exit(1);
    # }
    # $::my_server_pub_key=$b;
# }

# Now open the client socket on 44422 and generate a
# packet of data to send....

{
    my $port = 44422;
    if ($port =~ /\D/) { $port = getservbyname($port, 'tcp') }
    die "No port" unless $port;
    $iaddr   = inet_aton('localhost') or die;
    $paddr   = sockaddr_in($port, $iaddr);
    $proto   = getprotobyname('tcp');
    socket($fd, PF_INET, SOCK_STREAM, $proto)  || die;
    connect($fd, $paddr)    || die;
    my ($a,$b)=gri2_rsatcp::client_send_data({
        fd              => $fd,
        'username/host' => 'myuid/myhost',
        private         => $::private,
        message         => 'hello world'
    });
    if($a==0) {
        print STDERR "Error: $_\n" foreach(@$b);
        exit(1);
    }
    # Data has been sent ... so we have have the session key to decode the response
    print "session_key=",Dumper($b);
    my $session_key=$b;
    ($a,$b)=gri2_rsatcp::client_recv_data({
        fd              => $fd,
        session_key     => $session_key,
    });
    if($a==0) {
        print STDERR "Error: $_\n" foreach(@$b);
        exit(1);
    }
    print "Success: $b\n";
}

__DATA__
# Ok for our testing we create a socket on port 44422 and
# listen for connections. 
{
    my $port=44422;
    my $proto = getprotobyname('tcp');
    socket(Server, PF_INET, SOCK_STREAM, $proto) or die;
    setsockopt(Server, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) or die;
    bind(Server, sockaddr_in($port, INADDR_ANY)) or die;
    listen(Server,SOMAXCONN) or die;

    # Main LOOP...
    my $client_fd;
    for ( ; $paddr = accept($client_fd,Server); close $client_fd) {
        my($port,$iaddr) = sockaddr_in($paddr);
        my $name = gethostbyaddr($iaddr,AF_INET);

        # read in the request...
        my ($a,$b,$c)=gri2_rsatcp::recv_data({
            fd=>$client_fd, 
            private=>$::private,
        });
        if(! $a) {
            print STDERR "Error: $_\n" foreach(@$b); next;
        }
        print "Ident: $b\n";
        print "Msg  : $c\n";
    }
}

